package xuhaijia.lanou3g.autohome.activity;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseActivity;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.find.RadioAdapter;
import xuhaijia.lanou3g.autohome.find.RadioBean;

/**
 * Created by 7 on 16/8/27.
 */
public class RadioActivity extends BaseActivity implements View.OnClickListener {

    private ListView radioLv;
    String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private TextView closeTv;
    private RadioAdapter radioAdapter;
    private ImageView playingIv;
    public TextView namePlaying;


    @Override
    protected int setLayout() {
        return R.layout.activity_radio;
    }

    @Override
    protected void initView() {
        radioLv = bindView(R.id.activity_radio_lv);
        closeTv = bindView(R.id.activity_radio_close);
        closeTv.setOnClickListener(this);
        playingIv = bindView(R.id.activity_radio_playing);
        namePlaying = bindView(R.id.activity_radio_name);

    }

    @Override
    protected void initData() {
        playingIv.setImageResource(R.drawable.playing);
        AnimationDrawable animationDrawable = (AnimationDrawable) playingIv.getDrawable();
        animationDrawable.start();
        radioAdapter = new RadioAdapter(this);

        mNetTool.getNetData(URLValues.RADIO_STATION_URL, RadioBean.class, new NetTool.NetListener<RadioBean>() {
            @Override
            public void onSuccess(RadioBean radioBean) {
                for (int i = 0; i < radioBean.getResult().getList().size(); i++) {
                    String startTime = radioBean.getResult().getList().get(i).getStarttime();
                    long startTimeL = stringToLong(startTime);
                    String endTime = radioBean.getResult().getList().get(i).getEndtime();
                    long endTimeL = stringToLong(endTime);
                    long nowTime = System.currentTimeMillis();
                    if (nowTime < endTimeL && nowTime > startTimeL) {
                        namePlaying.setText("正在播放:  " + radioBean.getResult().getList().get(i).getAlbum());
                    }
                }
                radioAdapter.setRadioBean(radioBean);
                radioLv.setAdapter(radioAdapter);

                RadioEvent radioEvent = new RadioEvent();
                radioEvent.setUrl(radioBean.getResult().getRadioinfourl());
                EventBus.getDefault().post(radioEvent);

            }

            @Override
            public void onError(String errorMsg) {

            }
        });



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_radio_close:
                finish();
                break;
        }
    }

    private long stringToLong(String time) {
        try {
            if (time != null) {
                SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT);
                return format.parse(time).getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }
}
